package com.loooooo.pm2024.modules.sys.service;


import com.loooooo.pm2024.modules.sys.mapper.DictCatalogMapper;
import com.loooooo.pm2024.modules.sys.mapper.DictMapper;
import com.loooooo.pm2024.modules.sys.mapper.UserMapper;
import com.loooooo.pm2024.modules.sys.model.Dict;
import com.loooooo.pm2024.modules.sys.model.DictCatalog;
import com.loooooo.pm2024.modules.sys.model.User;
import com.loooooo.pm2024.modules.sys.selector.DictCatalogSelector;
import com.loooooo.pm2024.modules.sys.selector.DictSelector;
import com.loooooo.pm2024.modules.sys.selector.UserSelector;
import com.loooooo.pm2024.security.PasswordEncoder;
import com.loooooo.pm2024.security.SecurityUtils;
import com.loooooo.pm2024.service.IdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DictCatalogService {

    @Autowired
    DictCatalogMapper catalogMapper;
    @Autowired
    DictService dictService;
    @Autowired
    IdService idService;


    public List<DictCatalog> list(DictCatalogSelector selector) {
        if (selector != null) {
            if (StringUtils.hasText(selector.getOrderBy())) {

            }
            selector.setNow(System.currentTimeMillis());
        }
        List<DictCatalog> list = catalogMapper.select(selector);
        if (list.size() > 0 && selector != null) {
            if (Objects.equals(1, selector.getWithDictList())) {
                DictSelector dictSelector = new DictSelector();
                dictSelector.setCatalogIdList(list.stream().map(x -> x.getId()).collect(Collectors.toList()));
                List<Dict> ls = dictService.list(dictSelector);
                if (ls.size() > 0) {
                    for (DictCatalog catalog : list) {
                        catalog.setDictList(ls.stream().filter(x -> Objects.equals(x.getCatalogId(), catalog.getId())).collect(Collectors.toList()));
                    }
                }
            }
        }
        return list;
    }

    public int count(DictCatalogSelector selector) {
        return catalogMapper.count(selector);
    }

    public int update(DictCatalog dictCatalog) {
        long now = System.currentTimeMillis();
        Long userId = SecurityUtils.loginedUserId();
        dictCatalog.setUpdatedAt(now);
        dictCatalog.setUpdatedBy(userId);

        if (dictCatalog.getId() == null || dictCatalog.isNewEntity() || dictCatalog.getId() <= 0L) {
            if (dictCatalog.getId() == null || dictCatalog.getId() <= 0L) {
                dictCatalog.setId(idService.nextId());
            }
            dictCatalog.setCreatedAt(now);
            dictCatalog.setCreatedBy(userId);
            catalogMapper.insert(dictCatalog);
        }
        if (dictCatalog.getDictList() != null) {
            for (Dict dict : dictCatalog.getDictList()) {
                dict.setCatalogId(dictCatalog.getId());
                dict.setCatalog(dictCatalog.getCode());
                dictService.update(dict);
            }
        }
        return catalogMapper.update(dictCatalog);
    }
}
